home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16064 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  41 lines

  1. Newsgroups: comp.lang.c++
  2. Path: thor.cf.ac.uk!news
  3. From: ghassempoory@cf.ac.uk (M. Ghassempoory)
  4. Subject: Re: Small question
  5. Sender: news@cf.ac.uk (USENET News System)
  6. Message-ID: <DpLH0F.DzI@cf.ac.uk>
  7. Date: Tue, 9 Apr 1996 12:21:51 GMT
  8. X-Nntp-Posting-Host: giap.elsy.cf.ac.uk
  9. Content-Type: Text/Plain; charset=US-ASCII
  10. References: <31674eeb.132847918@vixa.voyager.net>
  11. Mime-Version: 1.0
  12. Organization: ENGIN - UWCC
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <31674eeb.132847918@vixa.voyager.net>, sence@ava.taby.se says...
  16. >
  17. >If I have 2 cpp files and i use the same global variable that i define
  18. >in some file that both cpp-files includes. How do I tell the compiler
  19. >that the variable is the same and that it shouldn't make space 2
  20. >times?
  21. >
  22. >
  23. >
  24. >  // bjorn@ava.taby.se
  25. >  // http://www.ava.taby.se/www/sence
  26.  
  27.  
  28. Most C++/C compilers do not mind this and are clever enough 
  29. to link with only one reference to this global variable. The
  30. proper way of doing it is to define the variable in the include 
  31. file like this:
  32.  
  33. extern int globalVar;
  34.  
  35.  
  36. and then in one of your cpp files which include this do the following
  37. at the top of the module after includes:
  38.  
  39. int globalVar = 5;
  40.  
  41.